package local.blueheart.data.springdatademo.util.mht;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Properties;

public class MimeMessageBuilder {

    private static final Logger LOGGER = LoggerFactory.getLogger(MimeMessageBuilder.class);

    private static final String smtpHost = "localhost";

    private MimeMessage mimeMessage;

    private MimeMultipart mimeMultipart;

    private MimeMessageBuilder() {
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.mime.allowutf8",true);
        Session session = Session.getDefaultInstance(props, null);
        mimeMessage = new MimeMessage(session);
    }

    public static MimeMessageBuilder defaultInstance(){
        return new MimeMessageBuilder();
    }

    public MimeMessageBuilder setFrom(String from){
        try {
            mimeMessage.setFrom(from);
        } catch (MessagingException e) {
            LOGGER.error("mimeMessage设置错误",e);
        }finally {
            return this;
        }
    }

    public  MimeMessageBuilder setSubject(String subject,String encoding){
        try {
            mimeMessage.setSubject(subject,encoding);
        } catch (MessagingException e) {
            LOGGER.error("mimeMessage设置错误",e);
        }finally {
            return this;
        }
    }

    public MimeMessageBuilder setHeader(String name,String value){
        try {
            mimeMessage.setHeader(name,value);
        } catch (MessagingException e) {
            LOGGER.error("mimeMessage设置错误",e);
        }finally {
            return this;
        }
    }

    public MimeMessageBuilder setMimeMultiPart(String subtype){
        mimeMultipart = new MimeMultipart(subtype);
        return this;
    }

    public MimeMessageBuilder setHTMLBodyPart(String htmlText, String encoding, Map<String,String> headers){
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        try {
            mimeBodyPart.setText(htmlText,encoding);
            if(headers!=null && !headers.isEmpty()){
                for(Map.Entry<String,String > entry:headers.entrySet()){
                    mimeBodyPart.setHeader(entry.getKey(),entry.getValue());
                }
            }
            mimeMultipart.addBodyPart(mimeBodyPart);
        } catch (MessagingException e) {
            LOGGER.error("mimeMessage设置错误",e);
        }finally {
            return this;
        }
    }

    public MimeMessageBuilder addBodyPart(String location,String encoding, DataSource dataSource){
        MimeBodyPart bp = new MimeBodyPart();
        try {
            bp.addHeader("Content-Location",
                    javax.mail.internet.MimeUtility.encodeWord(java.net.URLDecoder.decode(location, encoding)));
            bp.setDataHandler(new DataHandler(dataSource));
            mimeMultipart.addBodyPart(bp);
        } catch (MessagingException e) {
            LOGGER.error("mimeMessage设置错误",e);
        } catch (UnsupportedEncodingException e) {
            LOGGER.error("编码转换错误",e);
        }finally {
            return this;
        }
    }


    public MimeMessage build(){
        try {
            mimeMessage.setContent(mimeMultipart);
        } catch (MessagingException e) {
            LOGGER.error("mime内容设置错误",e);
        }finally {
            return mimeMessage;
        }

    }



}
